package com.kfc.platform.transaction.service.impl;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kfc.platform.transaction.dao.TradeHistoryDao;
import com.kfc.platform.transaction.dao.TransactionDao;
import com.kfc.platform.transaction.dao.impl.TransactionDaoImpl;
import com.kfc.platform.transaction.domain.TradeHistory;
import com.kfc.platform.transaction.domain.TradeList;
import com.kfc.platform.transaction.service.TransactionService;
import com.kfc.platform.user.dao.UserDao;
import com.kfc.platform.user.domain.User;

public class TransactionServiceImpl implements TransactionService{
	private TransactionDao transactionDao;
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String date = df.format(new Date());
	public void setTransactionDao(TransactionDao transactionDao) {
		this.transactionDao = transactionDao;
	}
	public TransactionDao getTransactionDao() {
		return transactionDao;
	}
	private TradeHistoryDao tradehistoryDao ;

	public TradeHistoryDao getTradehistoryDao() {
		return tradehistoryDao;
	}

	public void setTradehistoryDao(TradeHistoryDao tradehistoryDao) {
		this.tradehistoryDao = tradehistoryDao;
	}
	@Override
	public String createTransaction(TradeList tradeList) {
//		// TODO Auto-generated method stub
		TradeList insertTransaction = new TradeList();
		insertTransaction.setAmount(tradeList.getAmount());
		insertTransaction.setPrice(tradeList.getPrice());
		insertTransaction.setTrade_date(tradeList.getTrade_date());
		insertTransaction.setUsername(tradeList.getUsername());
		insertTransaction.setType(tradeList.getType());
		insertTransaction.setStatus(tradeList.getStatus());
		//以下是插入交易记录表
		TradeHistory inserthistory = new TradeHistory();
		inserthistory.setAmount(tradeList.getAmount());
		inserthistory.setPrice(tradeList.getPrice());
		inserthistory.setList_time(tradeList.getTrade_date());
		//inserthistory.setTrade_date(new SimpleDateFormat("yy-mm-dd hh:mm:ss").format(new Date()));
		inserthistory.setUsername(tradeList.getUsername());
		inserthistory.setType(tradeList.getType());
		inserthistory.setStatus(tradeList.getStatus());
		//inserthistory.setTransaction(transaction);
		if(tradeList == null){
			throw new IllegalArgumentException("transaction must be not null.");
		}
		transactionDao.save(insertTransaction);
		//tradehistoryDao.save(inserthistory);
		//System.out.println("status："+inserthistory.getStatus());
		return "插入数据库成功！";
	}
	@Override
	public void getTransaction() {
		// TODO Auto-generated method stub
		List<TradeList> ls = new ArrayList<TradeList>();
		ls = transactionDao.findAll();
		if(!ls.isEmpty())
			for(TradeList ts:ls)
				System.out.println("amount:"+ts.getAmount());
	}
	@Override
	public TradeList findTransactionbyId(int id) {
		// TODO Auto-generated method stub
		TradeList selectTransaction = new TradeList();
		selectTransaction = transactionDao.findById(id);
//		System.out.println("id:"+selectTransaction.getListnum());
//		System.out.println("date:"+selectTransaction.getTrade_date());
//		System.out.println("amount:"+selectTransaction.getAmount());
//		System.out.println("type:"+selectTransaction.getType());
//		System.out.println("status:"+selectTransaction.getStatus());
		return selectTransaction;
	}
	@Override
	public TradeList insertgetId(TradeList tradeList) {
		// TODO Auto-generated method stub
		TradeList insertTransaction = new TradeList();
		insertTransaction.setAmount(tradeList.getAmount());
		insertTransaction.setPrice(tradeList.getPrice());
		insertTransaction.setTrade_date(tradeList.getTrade_date());
		insertTransaction.setUsername(tradeList.getUsername());
		insertTransaction.setType(tradeList.getType());
		insertTransaction.setStatus(tradeList.getStatus());
		insertTransaction.setSum(tradeList.getSum());
		return transactionDao.insertgetId(insertTransaction);
	}
	@Override
	public void updatebyId(int id, BigDecimal num) {
		// TODO Auto-generated method stub
		transactionDao.updatebyId(id, num);
	}
	@Override
	public void update(TradeList tradeList) {
		// TODO Auto-generated method stub
		transactionDao.update(tradeList);
	}
	@Override
	public int savegetid(TradeList tradeList) {
		// TODO Auto-generated method stub
		return transactionDao.savegetid(tradeList);
	}
	@Override
	public List<TradeList> getbytype(String type) {
		// TODO Auto-generated method stub
		return transactionDao.findlistbytype(type);
	}
}
